const WebSocket = require('ws');

// 测试配置
const config = {
  wsUrl: 'ws://localhost:3000',
  userId: 1,
  characterId: 1
};

// 测试数据
const testCases = [
  {
    name: '查询背包药品信息 (1416)',
    code: 1416,
    payload: { pageNum: 0 }
  },
  {
    name: '查询背包装备信息 (1423)',
    code: 1423,
    payload: { pageNum: 0 }
  },
  {
    name: '查询背包石头 (6034)',
    code: 6034,
    payload: { pageNum: 0 }
  },
  {
    name: '查询背包杂物 (1425)',
    code: 1425,
    payload: { pageNum: 0 }
  },
  {
    name: '查询药品信息 (210)',
    code: 210,
    payload: { no: 1 }
  },
  {
    name: '查询钱庄金币信息 (1450)',
    code: 1450,
    payload: { pageNum: 0 }
  },
  {
    name: '查询金币市场交易信息 (1455)',
    code: 1455,
    payload: { type: 1 }
  },
  {
    name: '查询角色首页信息 (1000)',
    code: 1000,
    payload: null
  },
  {
    name: '查询副将战斗技能 (1006)',
    code: 1006,
    payload: null
  },
  {
    name: '查询体力值状态 (1130)',
    code: 1130,
    payload: null
  },
  {
    name: '查询技能列表 (1382)',
    code: 1382,
    payload: null
  },
  {
    name: '查询VIP信息 (6102)',
    code: 6102,
    payload: { lvl: -2 }
  },
  {
    name: '查询签到信息 (5309)',
    code: 5309,
    payload: { roleId: 1 }
  },
  {
    name: '查询积分商店 (5311)',
    code: 5311,
    payload: { pageNum: 0 }
  },
  {
    name: '查询宝库宝物 (1120)',
    code: 1120,
    payload: { pageNum: 0 }
  },
  {
    name: '查询宝库经验书 (1199)',
    code: 1199,
    payload: { pageNum: 0 }
  }
];

async function testBackpackFunctions () {
  console.log('🚀 开始测试背包相关功能...\n');

  const ws = new WebSocket(config.wsUrl);

  ws.on('open', async () => {
    console.log('✅ WebSocket 连接已建立');

    // 发送认证消息
    const authMessage = {
      c: 1,
      d: {
        userId: config.userId,
        token: 'test-token'
      }
    };

    ws.send(JSON.stringify(authMessage));

    // 等待认证完成后再开始测试
    setTimeout(() => {
      runTests();
    }, 1000);
  });

  ws.on('message', (data) => {
    try {
      const response = JSON.parse(data.toString());
      console.log(`📥 收到响应: ${JSON.stringify(response, null, 2)}\n`);
    } catch (error) {
      console.log(`📥 收到原始数据: ${data.toString()}\n`);
    }
  });

  ws.on('error', (error) => {
    console.error('❌ WebSocket 错误:', error);
  });

  ws.on('close', () => {
    console.log('🔌 WebSocket 连接已关闭');
  });

  function runTests () {
    console.log('🧪 开始执行测试用例...\n');

    testCases.forEach((testCase, index) => {
      setTimeout(() => {
        console.log(`\n--- 测试 ${index + 1}: ${testCase.name} ---`);
        console.log(`📤 发送请求: code=${testCase.code}, payload=${JSON.stringify(testCase.payload)}`);

        const message = {
          c: testCase.code,
          d: testCase.payload
        };

        ws.send(JSON.stringify(message));
      }, index * 2000); // 每2秒发送一个测试请求
    });

    // 测试完成后关闭连接
    setTimeout(() => {
      console.log('\n✅ 所有测试用例已发送完成');
      setTimeout(() => {
        ws.close();
      }, 5000);
    }, testCases.length * 2000 + 2000);
  }
}

// 运行测试
testBackpackFunctions().catch(console.error); 